package com.up.api.config;

import com.up.api.annotion.Remoting;
import com.up.api.remote.Interface1;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author: liuchengjun
 * @title: RemoteConfig
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2023/12/13/013 11:47 (星期三)
 * @since V2.0
 */
@Component
public class RemoteConfig implements BeanPostProcessor, ApplicationContextAware {
    private ApplicationContext applicationContext;
    private final static Boolean enableFeign = true;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> clazz = bean.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            Remoting customAnnotation = field.getAnnotation(Remoting.class);
            if (customAnnotation != null) {
//                Object implementationBean = applicationContext.getBean(implementationValue);
//                if (implementationBean == null) {
//                    throw new RuntimeException("No implementation found for: " );
//                }
                List<Interface1> interface1List = (List)applicationContext.getBean(field.getType());
//                try {
//                    field.set(bean, implementationBean);
//                } catch (IllegalAccessException e) {
//                    throw new RuntimeException("Error setting field value", e);
//                }
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
